<?php

namespace App\Http\Controllers;

use App\Article;
use App\Base;
use App\Comments;
use App\Game;
use App\Order;
use App\Recharge;
use App\Shop;
use App\Transfer;
use App\Users;
use Illuminate\Support\Facades\Input;
use Overtrue\Wechat\Auth;
use Overtrue\Wechat\Menu;
use Overtrue\Wechat\MenuItem;
use Overtrue\Wechat\Payment;
use Overtrue\Wechat\Payment\Business;
use Overtrue\Wechat\Payment\Notify;
use Overtrue\Wechat\Payment\UnifiedOrder;
use Overtrue\Wechat\Server;
use Session;

class WebController extends Controller
{

    public function test(Server $server)
    {
        var_dump('1');
        die;
    }

    public function barPay()
    {
        return view('web.barpay');
    }


    public function logger($log_content)
    {
        file_put_contents(base_path() . "/public/test.txt", date('H:i:s') . " " . $log_content . "\r\n", FILE_APPEND);
    }

    public function authorizes(Auth $auth)
    {
        if (!Session::has('logged_user')) {
            $user = $auth->authorize();
            Session::put('logged_user', $user->all());
            return redirect('/wx/index');
        } else {
            return redirect('/wx/index');
        }
    }

    public function index()
    {
        //var_dump(Session::all());
        //var_dump(Session::get('logged_user'));
        $openid = Session::get('logged_user')['openid'];
        if ($openid == '') {
            return redirect('/wx/authorizes');
        }
//        $condition=array(
//            'openid'=>$openid
//        );
//        $getData=getFromApi($condition);
//        $errcode=$getData->errcode;
//        if($errcode!=0){
//            $userss=Users::where('openid','=',$openid)->first();
//            if(count($userss)>0){
//                $userss->userid='';
//                $userss->phonenum='';
//                $userss->save();
//            }
//            return redirect('wx/login');
//       }
        $users = Users::where('openid', '=', $openid)->first();
        $userid = 0;
        $getData = (object)array();
        if (count($users) == 0) {
            $Users = new Users();
            $Users->nickname = Session::get('logged_user')['nickname'];
            $Users->headimgurl = Session::get('logged_user')['headimgurl'];
            $Users->openid = $openid;
            $Users->created_at = time();
            $Users->updated_at = time();
            $Users->save();
        } else {
            if ($users->userid) {
                $userid = $users->userid;
                $action = '/queryByIdCard';
                $condition = array(
                    'idcard' => $userid,
                    'shopId' => 4
                );
                $getData = getFromApi($action, $condition);
            }
        }
//        var_dump($getData);die;
        $getData->nickname = Session::get('logged_user')['nickname'];
        $getData->userid = $userid;

//        $users->nickname=Session::get('logged_user')['nickname'];
//        $users->headimgurl=Session::get('logged_user')['headimgurl'];
//        $users->save();
        //echo json_encode($getData);
        //var_dump($getData->data);
        $home = Base::where('name', '=', 'home')->first();
        return view('web.index')->with('getData', $getData)->with('home', $home);
    }

    public function game()
    {
        $games = Game::all();
        return view('web.game')->with('games', $games);
    }

    public function activity()
    {
        $cid = \Input::get('cid');
        $activitys = '';
        if ($cid) {
            $activitys = Article::where('cid', '=', $cid)->get();
        } else {
            $activitys = Article::all();
        }
        return view('web.activity')->with('activitys', $activitys);
    }

    public function activityinfo()
    {
        $id = \Input::get('id');
        $article = Article::find($id);
        return view('web.activityinfo')->with('article', $article);
    }

    public function commentindex()
    {
        $comments = Comments::orderBy('created_at', 'desc')->paginate(50);
        $datas = array();
        foreach ($comments as $v) {
            //$data[]=$v;
            $user = Users::find($v->u_id);
            $data = array();
            $data = $v;
            $data['nickname'] = $user->nickname;
            $data['headimgurl'] = $user->headimgurl;
            $datas[] = $data;
        }
        return view('web.commentindex')->with('comments', $datas);
    }

    public function comment()
    {
        return view('web.comment');
    }

    public function docomment()
    {
        $openid = Session::get('logged_user')['openid'];
        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
        $users = Users::where('openid', '=', $openid)->first();
        if (count($users) == 0) {
            echo json_encode($users);
            return false;
        }
        $u_id = $users->id;
        $content = \Input::get('content');
        $Comments = new Comments();
        $Comments->content = $content;
        $Comments->u_id = $u_id;
        $Comments->save();
        $data = array();
        $data['errmsg'] = 0;
        $data['msg'] = '提交成功！';
        $data['data'] = array();
        echo json_encode($data);
    }


    public function transfer()
    {
        $openid = Session::get('logged_user')['openid'];
        $users = Users::where('openid', '=', $openid)->first();
        $userid = $users->userid;
        $action = '/queryByIdCard';
        $condition = array(
            'idcard' => $userid,
            'shopId' => 4
        );
        $getData = getFromApi($action, $condition);
//        if(!$openid){
//            return redirect('wx/login');
//        }
//        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
//        $action='/GetUserInFo.do';
//        $condition=array(
//            'openid'=>$openid
//        );
//        $getData=getFromApi($action,$condition);
        $mybar = $getData->data;
        /*$action='/getAllNetBar.do';
        $condition=array();
        $allBars=getFromApi($action,$condition);*/


        return view('web.transfer')->with('allBars', $mybar)->with('mybar', $mybar);
    }

    public function dotransfer()
    {
        $openid = Session::get('logged_user')['openid'];
        $users = Users::where('openid', '=', $openid)->first();
        $userid = $users->userid;
//        if(!$openid){
//            return redirect('wx/login');
//        }
        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
        $sourceid = \Input::get('sourceid');
        $descid = Input::get('descid');
        $amount = Input::get('amount');
        $amount = $amount * 100;
        $ispersent = false;
        $action = '/roam';
        $condition = array(
            'idcard' => $userid,
            'ispersent ' => $ispersent,
            'sourceid' => $sourceid,
            'descid' => $descid,
            'amount' => $amount,
            'memo' => '网咖转账'
        );
//        $users=Users::where('openid','=',$openid)->first();
//        if(count($users)==0){
//            echo json_encode($users);
//            return false;
//        }
        $u_id = $users->id;
        $transfer = new Transfer();
        $transfer->u_id = $u_id;
        $transfer->sourceid = $sourceid;
        $transfer->descid = $descid;
        $transfer->amount = $amount;
        $transfer->status = 0;
        $transfer->created_at = time();
//        var_dump($transfer->u_id,$transfer->sourceid,$transfer->descid,$transfer->amount,$transfer->status,$transfer->created_at,$transfer->updated_at);die;
        $transfer->save();
        $result = getFromApi($action, $condition);
        if ($result->ret == 0) {
            $transfer->status = 1;
            $transfer->remark = '转账成功';
        } else {
            $transfer->remark = $result->message;
        }
        $transfer->updated_at = time();
        $transfer->save();
        echo json_encode($result);
        //return redirect()->back();
    }

    public function login()
    {
        /*$openid=Session::get('logged_user')['openid'];
        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
        if(!$openid){
            return redirect('wx/login');
        }*/
        return view('web.login');
    }

    public function getcode()
    {
        $openid = Session::get('logged_user')['openid'];
        $phonenum = \Input::get('phonenum');
        $userCode = \Config::get('wechat.userCode');
        $userPass = \Config::get('wechat.userPass');
        $Channel = \Config::get('wechat.Channel');
        $Msg = \Config::get('wechat.Msg');
        $code = $this->createcode();

        Session::set($openid . 'phoneCode', $code);
        Session::set($openid . 'verifphone', $phonenum);

        $Msg = str_replace("#", $code, $Msg);
        $action = '/SendMsg';
        $condition = array(
            'userCode' => $userCode,
            'userPass' => $userPass,
            'DesNo' => $phonenum,
            'Msg' => $Msg,
            'Channel' => $Channel
        );
        $getData = getFromApi($action, $condition, 'phonecode');
        $getData = json_decode($getData, true);

        echo json_encode(array('ret' => $getData));
    }


    public function createcode($length = 6)
    {
        return rand(pow(10, ($length - 1)), pow(10, $length) - 1);
    }

    public function doLogin()
    {
        /*if (!Session::has('logged_user')) {
            return redirect('/wx/authorizes');
        }*/
        $data = array();
        $userid = \Input::get('userid');
        $name = \Input::get('name');
        $phonenum = \Input::get('phonenum');
        $code = \Input::get('code');
        if ($userid == '' || $phonenum == '') {
            //return redirect('/wx/login');
            $data['errcode'] = 1;
            $data['errmsg'] = '请输入身份证和手机号';
            $data['data'] = '';
            echo json_encode($data);
            die();
        }
        $openid = Session::get('logged_user')['openid'];
        if ($openid == '') {
            $data['errcode'] = 1;
            $data['errmsg'] = '请重新打开！';
            $data['data'] = '';
            echo json_encode($data);
            die();
        }
        $users = Users::where('openid', '=', $openid)->first();
        $phoneCode = Session::get($openid . 'phoneCode');
        $verifphone = Session::get($openid . 'verifphone');
        if (empty($verifphone) || empty($phoneCode)) {
            $data['errcode'] = 1;
            $data['errmsg'] = '验证码已过期';
            $data['data'] = '';
            echo json_encode($data);
            die();
        }
        if (($code == $phoneCode) && ($phonenum == $verifphone)) {
            $users->userid = $userid;
            $users->nickname = $name;
            $users->phonenum = $phonenum;
            $users->save();
            $data['errcode'] = 0;
            $data['errmsg'] = 'success';
            $data['data'] = '/wx/index';
            echo json_encode($data);
            die();
        } else {
            $data['errcode'] = 1;
            $data['errmsg'] = '手机验证错误';
            $data['data'] = '';
            echo json_encode($data);
            die();
        }


//        $action='/weChatBind';
//        $condition=array(
//            'userid'=>$userid,
//            'phonenum'=>$phonenum,
//            'openid'=>$openid,
//            'name'=>$name,
//            'code'=>$code
//        );
//
//        $getData=getFromApi($action,$condition);
//
//        $errcode=$getData->errcode;

//        if($errcode==0){
//            $users=Users::where('openid','=',$openid)->first();
//            if(count($users)==0){
//                $Users=new Users();
//                $Users->userid=$userid;
//                $Users->phonenum=$phonenum;
//                $Users->openid=$openid;
//                $Users->save();
//            }else{
//                $users->userid=$userid;
//                $users->phonenum=$phonenum;
//                $users->save();
//            }
//            $data['errcode']=0;
//            $data['errmsg']='success';
//            $data['data']='/wx/index';
//            echo json_encode($data);
//            die();
//            //return redirect('/wx/index');
////        }
//        if($errcode==1){
//            $users=Users::where('openid','=',$openid)->first();
//            if(count($users)>0){
//                $users->userid='';
//                $users->phonenum='';
//                $users->save();
//            }
//        }
//        $data['errcode']=1;
//        $data['errmsg']=$getData->errmsg;
//        $data['data']='';
//        echo json_encode($data);
        //return redirect('/wx/login');
    }


    //解绑
    public function logout()
    {
        $action = '/weChatRemoveBind';
        $condition = array(
            'userid' => '330184199210032319',
            'phonenum' => '18868945382',
            'openid' => 'ox9a0tx6bkzc2U4loFNq3XDOr08w'
        );
        $getData = getFromApi($action, $condition);
        //echo json_encode($getData);
        $errcode = $getData->errcode;
        //echo $errcode=0;
        if ($errcode == 0) {
            $users = Users::where('openid', '=', 'ox9a0tx6bkzc2U4loFNq3XDOr08w')->first();
            $users->openid = '';
            $users->save();
            return redirect('/wx/login');
        }
    }


    public function test1()
    {
        /*$action='/addGold';
        $condition=array(
            'openid'=>'ox9a0tx6bkzc2U4loFNq3XDOr08w',
            'srcid'=>'10000',
            'orderid'=>'17',
            'gold'=>'100'
        );
        $getData=getFromApi($action,$condition);
        $errcode=$getData->errcode;
        echo json_encode($getData);*/

        $action = '/weChatRemoveBind';
        $condition = array(
            'userid' => '330621199301315770',
            'phonenum' => '18868945283',
            'openid' => 'ox9a0t7jSe9VCbyjKfxQo-8b_kHY'
        );
        $getData = getFromApi($action, $condition);
        echo json_encode($getData);

        /*$action='/weChatBind';
        $condition=array(
            'userid'=>'330621199301315770',
            'phonenum'=>'13248530767',
            'openid'=>'ox9a0tx6bkzc2U4loFNq3XDOr08w',
            'name'=>'金聪',
            'code'=>'12345'
        );

        $getData=getFromApi($action,$condition);

        echo json_encode($getData);*/

        //$errcode=$getData->errcode;
    }

    /**\
     * 自定义菜单
     * @throws \Overtrue\Wechat\Exception
     */
    public function setWechatMenu()
    {

//        $appId  = 'wx0fc4b300058b437c';
//        $secret = '19d8419e7c2b089a69e48203231ed125';
        $appId = \Config::get('wechat.app_id');
        $secret = \Config::get('wechat.secret');

        $menuService = new Menu($appId, $secret);

        //$menus = \Input::get('menus'); // menus 是你自己后台管理中心表单post过来的一个数组

        $menus[] = array(
            "name" => "账号管理",
            "type" => "view",
            "key" => "http://sx.cloud3a.com/wx/index"
        );
        $menus[] = array(
            "name" => "附近网咖",
            "type" => "view",
            "key" => "http://sx.cloud3a.com/wx/barList"
        );
        $menus[] = array(
            "name" => "留言咨询",
            "type" => "view",
            "key" => "http://sx.cloud3a.com/wx/comment"
        );
        /*$menus[]=array(
            "name"=>"测试1",
            "type"=>null,
            "key" =>null,
            "buttons"=>array(
                array(
                    "name"=>"GitHub",
                    "type"=>"view",
                    "key" =>"https://github.com/"
                ),
                array(
                    "name"=>"微博",
                    "type"=>"view",
                    "key" =>"http://dw.ala90.com/wx/authorizes"
                )
            )
        );*/

        $target = [];

        // 构建你的菜单
        foreach ($menus as $menu) {
            // 创建一个菜单项
            $item = new MenuItem($menu['name'], $menu['type'], $menu['key']);

            // 子菜单
            if (!empty($menu['buttons'])) {
                $buttons = [];
                foreach ($menu['buttons'] as $button) {
                    $buttons[] = new MenuItem($button['name'], $button['type'], $button['key']);
                }

                $item->buttons($buttons);
            }

            $target[] = $item;
        }

        $menuService->set($target); // 失败会抛出异常
        echo 'success';
    }

    /**
     * 网咖列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function barList()
    {
        return view('web.barList');
    }

    /**
     * 网咖详细信息
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function barInfo()
    {
        return view('web.barInfo');
    }

    public function shop()
    {
        $openid = Session::get('logged_user')['openid'];
        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
        if (!$openid) {
            return redirect('wx/login');
        }
        $nowtime = time();
        $users = Users::where('openid', '=', $openid)->first();
        $shops = Shop::where('status', '=', 1)->where('is_del', '=', 0)->where('start_at', '<', $nowtime)->where('end_at', '>', $nowtime)->get();
        return view('web.shop')->with('shops', $shops)->with('users', $users);
    }

    public function myshop()
    {
        $openid = Session::get('logged_user')['openid'];
        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
        if (!$openid) {
            return redirect('wx/login');
        }
        $users = Users::where('openid', '=', $openid)->first();
        $shops = Order::where('u_id', '=', $users->id)->get();
        $nowtime = time();
        $shops = \DB::select('SELECT a.total_fee,a.is_used,b.* FROM `l_order` AS a LEFT JOIN l_shop AS b on a.s_id=b.id WHERE a.type=1  AND ((b.start_at<? AND b.end_at>?) OR a.is_used=1) and a.u_id=? ORDER BY a.id DESC ', [$nowtime, $nowtime, $users->id]);
        //$shops=\DB::select('SELECT * FROM `l_order` AS a LEFT JOIN l_shop AS b on a.s_id=b.id WHERE b.is_del=0 AND a.u_id=:id',[$users->id]);
        return view('web.myshop')->with('shops', $shops)->with('users', $users);
    }


    public function gshop()
    {
        $openid = Session::get('logged_user')['openid'];
        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
        if (!$openid) {
            return redirect('wx/login');
        }
        $users = Users::where('openid', '=', $openid)->first();
        //$shops=Order::where('u_id','=',$users->id)->get();
        $nowtime = time();
        $shops = \DB::select('SELECT a.total_fee,a.is_used,b.* FROM `l_order` AS a LEFT JOIN l_shop AS b on a.s_id=b.id WHERE a.type=1  and a.u_id=? AND a.is_used=0 AND b.end_at<?', [$users->id, $nowtime]);
        //$shops=\DB::select('SELECT * FROM `l_order` AS a LEFT JOIN l_shop AS b on a.s_id=b.id WHERE b.is_del=0 AND a.u_id=:id',[$users->id]);
        return view('web.gshop')->with('shops', $shops)->with('users', $users);
    }


    public function doshop()
    {
        $shop_id = \Input::get('shop_id');
        $openid = Session::get('logged_user')['openid'];
        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
        if (!$openid) {
            return redirect('wx/login');
        }
        $users = Users::where('openid', '=', $openid)->first();
        $data = array();
        if (count($users) > 0) {
            $shops = Shop::find($shop_id);
            $nowtime = time();
            if ($shops->is_del > 0) {
                $data['errmsg'] = 1;
                $message = '已经删除!';
                $data['msg'] = $message;
                echo json_encode($data);
                die();
            }
            if ($shops->status != 1) {
                $data['errmsg'] = 1;
                $message = '未发布!';
                $data['msg'] = $message;
                echo json_encode($data);
                die();
            }
            if (!$shops->start_at <= $nowtime && !$shops->end_at >= $nowtime) {
                $data['errmsg'] = 1;
                $message = '已经过期!';
                $data['msg'] = $message;
                echo json_encode($data);
                die();
            }
            if ($shops->score > $users->score) {
                $data['errmsg'] = 1;
                $message = '积分不足';
                $data['msg'] = $message;
                echo json_encode($data);
                die();
            }
            $users->score = $users->score - $shops->score;
            $users->save();
            $order = new Order();
            $order->s_id = $shops->id;
            $order->total_fee = $shops->score;
            $order->u_id = $users->id;
            $order->type = 1;
            $order->status = 1;
            $order->save();
            $shops->num = $shops->num - 1;
            $shops->save();
            $data['errmsg'] = 0;
            $data['msg'] = '兑换成功！';
            $data['data'] = $users->score;
            echo json_encode($data);
            die();
        }
        $data['errmsg'] = 1;
        $data['msg'] = '未登陆！';
        $data['data'] = array();
        echo json_encode($data);
    }

    public function recharge()
    {
        //$action='/getAllNetBar.do';
        //$condition=array();
        //$allBars=getFromApi($action,$condition);
        $openid = Session::get('logged_user')['openid'];
        $users = Users::where('openid', '=', $openid)->first();
        $userid = $users->userid;
        $action = '/queryByIdCard';
        $condition = array(
            'idcard' => $userid,
            'shopId' => 4
        );
        $getData = getFromApi($action, $condition);
        $mybar = $getData->data;

        $appId = \Config::get('wechat.app_id');
        $secret = \Config::get('wechat.secret');
        return view('web.recharge')->with('bars', $mybar);
    }


    public function dorecharge()
    {
        $appId = \Config::get('wechat.app_id');
        $secret = \Config::get('wechat.secret');
        $mch_id = \Config::get('wechat.mch_id');
        $mch_key = \Config::get('wechat.mch_key');
        $openid = Session::get('logged_user')['openid'];
        //$openid='ox9a0tx6bkzc2U4loFNq3XDOr08w';
//        if(!$openid){
//            return redirect('wx/login');
//        }
        $users = Users::where('openid', '=', $openid)->first();

        $out_trade_no = md5(uniqid() . microtime());
        $descid = Input::get('descid');
        $total_fee = Input::get('total_fee');
        $total_fee = $total_fee * 100;

        $recharge = new Recharge();
        $recharge->out_trade_no = $out_trade_no;
        $recharge->descid = $descid;
        $recharge->total_fee = $total_fee;
        $recharge->u_id = $users->id;
        $recharge->status = 0;
        $recharge->save();
        /**
         * 第 1 步：定义商户
         */
        $business = new Business($appId, $secret, $mch_id, $mch_key);
        /**
         * 第 2 步：定义订单
         */
        $order = new \Overtrue\Wechat\Payment\Order();
        $order->body = '网咖充值';
        $order->out_trade_no = $out_trade_no;
        $order->total_fee = $total_fee;    // 单位为 “分”, 字符串类型
        $order->openid = $openid;
        $order->notify_url = 'http://xichong.wangkalike.com/wx/notify';
        /**
         * 第 3 步：统一下单
         */
        $unifiedOrder = new UnifiedOrder($business, $order);
        /**
         * 第 4 步：生成支付配置文件
         */
        $payment = new Payment($unifiedOrder);
        echo $payment->getConfig();
    }


    public function notify()
    {
        $appId = \Config::get('wechat.app_id');
        $secret = \Config::get('wechat.secret');
        $mch_id = \Config::get('wechat.mch_id');
        $mch_key = \Config::get('wechat.mch_key');
        $notify = new Notify(
            $appId,
            $secret,
            $mch_id,
            $mch_key
        );
        $transaction = $notify->verify();
        $transaction = json_decode($transaction);
        if ($transaction) {
            $appid = $transaction->appid;
            $openid = $transaction->openid;
            $out_trade_no = $transaction->out_trade_no;
            $total_fee = $transaction->total_fee;
            $transaction_id = $transaction->transaction_id;
            if ($appid == \Config::get('wechat.app_id')) {
                $recharge = Recharge::where('out_trade_no', '=', $out_trade_no)->where('status', '=', 0)->first();
                if (count($recharge) > 0) {
                    $recharge->remark = '正在支付中';
                    $recharge->status = 2;//正在支付
                    $recharge->save();

                    $u_id = $recharge->u_id;
                    $user = Users::find($u_id);
                    $openid = $user->openid;
                    $userid = $user->userid;


                    $action = '/charge';
                    $condition = array(
                        'idcard' => $userid,
                        'ispersent' => false,
                        'sourceid' => 4,
                        'descid' => $recharge->descid,
                        'amount' => $total_fee,
                        'memo' => '充值成功'
                    );
                    foreach ($condition as $v) {
                        $this->logger($v);
                    }


                    $getData = getFromApi($action, $condition);
                    //echo json_encode($getData);
                    $errcode = $getData->ret;
                    //echo $errcode=0;
                    if ($errcode == 0) {
                        $user->score = $user->score + $total_fee;
                        $user->save();
                        $recharge->transaction_id = $transaction_id;
                        $recharge->remark = 'success';
                        if ($total_fee != $recharge->total_fee) {
                            $recharge->remark = 'total_fee原值' . $recharge->total_fee . '。total_fee金额不对，已经修正';
                            $recharge->total_fee = $total_fee;

                        }
                        $recharge->status = 1;
                        $recharge->save();
                        echo 'success';
                    } else {
                        $recharge->status = 3;
                        $recharge->remark = $getData->message;
                        $recharge->save();
                    }
                    //if($openid){ }
                }
            }
        } else {
            $notify->reply('FAIL', 'verify transaction error');
        }

        $recharge->info = $notify->reply();
        $recharge->save();


        /*$text=Input::all();
        foreach ($text as $key=>$value)
        {
            $this->logger("Key: $key; Value: $value");
         }*/
//        if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
//            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//            //$this->logger($postStr);
//
//            $xml = simplexml_load_string($postStr);
//            $appid=$xml->appid;
//            $openid=$xml->openid;
//            $out_trade_no=$xml->out_trade_no;
//            $total_fee=$xml->total_fee;
//            $transaction_id=$xml->transaction_id;
//            if($appid==\Config::get('wechat.app_id')){
//                $recharge=Recharge::where('out_trade_no','=',$out_trade_no)->where('status','=',0)->first();
//                if(count($recharge)>0){
//                    $recharge->remark='正在支付中';
//                    $recharge->status=2;//正在支付
//                    $recharge->save();
//
//                    $u_id=$recharge->u_id;
//                    $user=Users::find($u_id);
//                    $openid=$user->openid;
//                    $userid=$user->userid;
//
//
//                    $action='/charge';
//                    $condition=array(
//                        'idcard'=>$userid,
//                        'ispersent'=>false,
//                        'sourceid'=>4,
//                        'descid'=>$recharge->descid,
//                        'amount'=>$total_fee,
//                        'memo'=>'充值成功'
//                    );
//                    foreach($condition as $v){
//                        $this->logger($v);
//                    }
//
//
//                    $getData=getFromApi($action,$condition);
//                    //echo json_encode($getData);
//                    $errcode=$getData->ret;
//                    //echo $errcode=0;
//                    if($errcode==0){
//                        $user->score=$user->score+$total_fee;
//                        $user->save();
//                        $recharge->transaction_id=$transaction_id;
//                        $recharge->remark='success';
//                        if($total_fee!=$recharge->total_fee){
//                            $recharge->remark='total_fee原值'.$recharge->total_fee.'。total_fee金额不对，已经修正';
//                            $recharge->total_fee=$total_fee;
//
//                        }
//                        $recharge->info=$postStr;
//                        $recharge->status=1;
//                        $recharge->save();
//                        echo 'success';
//                    }else{
//                        $recharge->status=3;
//                        $recharge->remark=$getData->message;
//                        $recharge->save();
//                    }
//                    //if($openid){ }
//                }
//            }
//
//        }
        //file_put_contents(base_path()."/public/test.txt",print_r($text,true) ,FILE_APPEND);
    }

    public function MP_verify()
    {
        $file_path = base_path("/MP_verify_VgzEduYSHtdcUfZs.txt");
        $myfile = fopen($file_path, "r") or die("Unable to open file!");
        echo fread($myfile, 16);
        fclose($myfile);
    }

    public function getBars()
    {
        $userid = \Input::get('userid');
        $action = '/queryByIdCard';
        $condition = array(
            'idcard' => $userid,
            'shopId' => 4
        );
        $getData = getFromApi($action, $condition);
        return view('web.getBars')->with('bars', $getData->data);

    }

    public function bookSeat()
    {
        $shopId = \Input::get('shopId');
        $action = '/queryPcList';
        $condition = array(
            'sourceid' => 4,
            'descid' => $shopId
        );
        $getData = getFromApi($action, $condition);
        return view('web.bookSeat')->with('seats', $getData->data);
    }

}
